-- ========================================================
-- @File	: router.lua
-- @Brief	: 路由器
-- @Author	: Leo Zhao
-- @Date	: 2016-05-05
-- ========================================================
router = router or {}

-- 路由分发
function router.dispatch()
	if _URL == '/' then
		if _CONF.default_page then redirect(_CONF.default_page);
		else router.welcome() end;
	else
		router.call();
	end
end

-- 调用相应的控制器方法
function router.call()
	require 'app/common';
	xpcall(function()
		local class = require('app/controllers/' .. _CONTROLLER);

		if type(class) ~= 'table' then
			error_handler.fatal("Do you forget reture controller at : " .. f .. "?");
		elseif class[_ACTION] and type(class[_ACTION]) == 'function' then
			class:init();
			class[_ACTION](class);
		else
			error_handler.fatal("Controller [" .. _CONTROLLER .. "] has no method named : " .. _ACTION);
		end
	end, function()
		error_handler.empty();
	end);	
end

-- 欢迎页
function router.welcome()
	local render = require('core.template');
	header("Content-type:text/html");
	render.render([[
		{(public/header.html)}
		<style>
			.welcome {
				font-family: Courier;
				font-size: 4em;
				margin-top: 20%;
				text-align: center;
			}
			.message {
				font-family: Courier;
				text-align: center;
			}
		</style>
		<div class="container">
			<h1 class="welcome"> Welcome To OmniWeb </h1>
			<hr><br>
			<label class="message float-xs-right"><i>———— TRY TO BE FASTER, SAMLLER & EASIER</i></label>
		</div>
		{(public/footer.html)}
	]], { title = "起始页" });
end